//
//  InSoundFootView.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/8.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundFootView.h"

#import "UIImageView+WebCache.h"

#import "InSoundFavouriteTool.h"

#import "InSoundDetail.h"


#import "InDownLoadSound.h"

#import "XCHudHelper.h"

#import "InSound.h"

#import "downLoad.h"

#import <UMSocial.h>


@interface InSoundFootView ()<UMSocialUIDelegate,UIAlertViewDelegate>

@property (nonatomic,strong)InSound *insound;

@property (nonatomic,copy)NSString *user_id;

@end

@implementation InSoundFootView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (NSString *)user_id {
    
    if (_user_id == nil) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        NSString *user_id = [userDefaults valueForKey:@"u_id"];
        
        _user_id = user_id;

    }
    return _user_id;

}

- (instancetype)init {
    self = [super init];
    
    if (self) {
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"InSoundFootView" owner:nil options:nil];
        self = array.lastObject;
    }
    return self;
}

- (void)loadImageWithStr:(NSString*)str WithImageView:(UIImageView *)imageView{
    
    NSURL *url = [NSURL URLWithString:str];
    
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    
}

- (void)setSoundDetail:(InSoundDetail *)soundDetail {
    
    
    [self loadImageWithStr:soundDetail.sound_channel_icon WithImageView:self.channelImageView];
    
    self.channelNameLabel.text = soundDetail.sound_channel_name;
    
    self.commentNumberLabel.text = soundDetail.sound_comment_count;
    
    self.loveNumberLabel.text = soundDetail.sound_like_count;
    
    self.shareNumberLabel.text = soundDetail.sound_share_count;
    
    self.downNumberLabel.text = soundDetail.sound_download_count;
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    NSLog(@"   user_id  %@",user_id);
    
    BOOL success = [InSoundFavouriteTool selectSound:soundDetail.sound_id WithUser_id:user_id];
    if (success) {
        self.loveBtn.selected = YES;
    }
    

    
//    self.insound.inSound_icon = soundDetail.sound_icon;
//    
//    self.insound.sound_id = soundDetail.sound_id;
//    
//    self.insound.inSound_name = soundDetail.sound_name;
//    
//    self.insound.inSound_Url = soundDetail.sound_URL;
    
}



//喜欢
- (IBAction)loveBtnClick:(id)sender {
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//    
//   // [userDefault removeObjectForKey:@"u_id"];
//    [userDefault setValue:@"222" forKey:@"u_id"];
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    NSLog(@"user_id%@",self.user_id);
    
    if (self.user_id != nil) {
        if (self.loveBtn.selected == NO) {
            self.loveBtn.selected = YES;
            self.loveNumberLabel.text = [NSString stringWithFormat:@"%d",self.loveNumberLabel.text.intValue+1];
            
            [self showMassageWithString:@"喜欢成功"];
            
        }else {
            self.loveBtn.selected = NO;
            self.loveNumberLabel.text = [NSString stringWithFormat:@"%d",self.loveNumberLabel.text.intValue - 1];
            
            [self showMassageWithString:@"已取消"];
            NSLog(@"   userid     %@",self.user_id);
        }

    }else {
        [self.delegate showAlertView];
    }
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}

//下载
- (IBAction)downBtnClick:(id)sender {
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//    
//    [userDefault removeObjectForKey:@"u_id"];
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    if (self.user_id != nil) {
        if ([self.delegate respondsToSelector:@selector(downLoad)]) {
            [self.delegate downLoad];
            [self showMassageWithString:@"正在努力下载中"];
        }

    }else {
        [self.delegate showAlertView];
    }
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
    
}

//分享
- (IBAction)shareBtnClick:(id)sender {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    if (self.user_id != nil) {
        if (self.shareBtn.selected == NO) {
            self.shareBtn.selected = YES;
            self.shareNumberLabel.text = [NSString stringWithFormat:@"%d",self.shareNumberLabel.text.intValue+1];
            [UMSocialSnsService presentSnsIconSheetView:[AppDelegate shareAppdelegate].window.rootViewController
                                                 appKey:@"507fcab25270157b37000010"
                                              shareText:@"你要分享的文字"
                                             shareImage:[UIImage imageNamed:@"icon.png"]
                                        shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                               delegate:self];
            
            [self showMassageWithString:@"分享成功"];
        }else {
            
            self.shareBtn.selected = NO;
            self.shareNumberLabel.text = [NSString stringWithFormat:@"%d",self.shareNumberLabel.text.intValue - 1];
        }
        
        NSLog(@"分享");
 
    }else {
        [self.delegate showAlertView];
    }
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}

- (void)showMassageWithString:(NSString *)string {
    [XCHudHelper showMessage:string];
}



@end
